package com.lrkj.payfee.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.lrkj.common.utils.DateUtils;
import com.lrkj.common.utils.SecurityUtils;
import com.lrkj.common.utils.uuid.IdUtils;
import com.lrkj.framework.utils.SerialNumUtil;
import com.lrkj.payfee.domain.HandAdvice;
import com.lrkj.payfee.domain.HandReceipt;
import com.lrkj.payfee.service.IHandAdviceService;
import com.lrkj.payfee.service.IHandReceiptService;
import com.lrkj.property.domain.Community;
import com.lrkj.property.service.ICommunityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lrkj.payfee.mapper.HandRecordMapper;
import com.lrkj.payfee.domain.HandRecord;
import com.lrkj.payfee.service.IHandRecordService;

/**
 * 交班管理Service业务层处理
 *
 * @author yajun_ren
 * @date 2022-03-30
 */
@Service
public class HandRecordServiceImpl implements IHandRecordService
{
    @Autowired
    private HandRecordMapper handRecordMapper;

    @Autowired
    private ICommunityService communityService;

    @Autowired
    private IHandReceiptService handReceiptService;

    @Autowired
    private IHandAdviceService handAdviceService;

    /**
     * 查询交班管理
     *
     * @param id 交班管理ID
     * @return 交班管理
     */
    @Override
    public HandRecord selectHandRecordById(String id)
    {
        return handRecordMapper.selectHandRecordById(id);
    }

    /**
     * 查询交班管理列表
     *
     * @param handRecord 交班管理
     * @return 交班管理
     */
    @Override
    public List<HandRecord> selectHandRecordList(HandRecord handRecord)
    {
        return handRecordMapper.selectHandRecordList(handRecord);
    }

    /**
     * 新增交班管理
     *
     * @param handRecord 交班管理
     * @return 结果
     */
    @Override
    public int insertHandRecord(HandRecord handRecord)
    {
        handRecord.setId(IdUtils.simpleUUID());
        handRecord.setCreateBy(SecurityUtils.getUsername());
        handRecord.setCreateTime(DateUtils.getNowDate());
        handRecord.setHandNo(SerialNumUtil.getNo("JK"));
        this.setUnitInfo(handRecord);
        this.createReceipt(handRecord);
        this.createAdvice(handRecord);
        return handRecordMapper.insertHandRecord(handRecord);
    }

    /**
     * 修改交班管理
     *
     * @param handRecord 交班管理
     * @return 结果
     */
    @Override
    public int updateHandRecord(HandRecord handRecord)
    {
        handRecord.setUpdateBy(SecurityUtils.getUsername());
        handRecord.setUpdateTime(DateUtils.getNowDate());
        this.setUnitInfo(handRecord);
        this.createReceipt(handRecord);
        this.createAdvice(handRecord);
        return handRecordMapper.updateHandRecord(handRecord);
    }

    /**
     * 批量删除交班管理
     *
     * @param ids 需要删除的交班管理ID
     * @return 结果
     */
    @Override
    public int deleteHandRecordByIds(String[] ids)
    {
        for (int i = 0; i < ids.length; i++) {
            handReceiptService.deletehandRecordId(ids[i]);
            handAdviceService.deletehandRecordId(ids[i]);
        }
        return handRecordMapper.deleteHandRecordByIds(ids);
    }

    /**
     * 删除交班管理信息
     *
     * @param id 交班管理ID
     * @return 结果
     */
    @Override
    public int deleteHandRecordById(String id)
    {
        handReceiptService.deletehandRecordId(id);
        handAdviceService.deletehandRecordId(id);
        return handRecordMapper.deleteHandRecordById(id);
    }

    @Override
    public List<HandRecord> onDayReceipt(HandRecord handRecord) {
        return handRecordMapper.onDayReceipt(handRecord);
    }

    @Override
    public List<HandRecord> onDayAdvanceReceipt(HandRecord handRecord) {
        return handRecordMapper.onDayAdvanceReceipt(handRecord);
    }

    @Override
    public int approve(HandRecord handRecord) {
        handRecord.setAuditState("20");
        handRecord.setAuditTime(DateUtils.getNowDate());
       return handRecordMapper.updateHandRecord(handRecord);
    }

    /**
     * 批量新增交班管理信息
     *
     * @param handRecords 交班管理对象
     */
    public int batchAddData(List<HandRecord> handRecords)
    {
           return handRecordMapper.batchAddData(handRecords);
    }

    private void setUnitInfo(HandRecord handRecord) {
        Community parentBean = communityService.selectCommunityById(handRecord.getCommunityId());
        handRecord.setAncestors(parentBean.getAncestors() + "," + parentBean.getId());
    }


    /**
     * 新增交班收款单明细
     * @param handRecord
     */
    private void createReceipt(HandRecord handRecord){
        if (handRecord.getHandReceiptList()!=null){
            handReceiptService.deletehandRecordId(handRecord.getId());
            List<HandReceipt> list =new ArrayList<>();
            handRecord.getHandReceiptList().stream().forEach(x->{
                HandReceipt handReceipt=new HandReceipt();
                handReceipt.setId(IdUtils.simpleUUID());
                handReceipt.setHandRecordId(handRecord.getId());
                handReceipt.setNo(x.getNo());
                handReceipt.setRoomName(x.getRoomName());
                handReceipt.setRoomId(x.getRoomId());
                handReceipt.setOwnerName(x.getOwnerName());
                handReceipt.setOwnerId(x.getOwnerId());
                handReceipt.setBillUser(x.getBillUser());
                handReceipt.setBillDate(x.getBillDate());
                handReceipt.setReceivableMoney(x.getReceivableMoney());
                handReceipt.setBillMoney(x.getBillMoney());
                handReceipt.setOddChangeMoney(x.getOddChangeMoney());
                handReceipt.setTransferAmount(x.getTransferAmount());
                handReceipt.setAuditStatus(x.getAuditStatus());
                handReceipt.setInvoiceStatus(x.getInvoiceStatus());
                handReceipt.setDocumentStatus(x.getDocumentStatus());
                handReceipt.setDiscount(x.getDiscount());
                list.add(handReceipt);
            });

            handReceiptService.doSaveList(list);
        }
    }

    /**
     * 新增交班付款单明细
     * @param handRecord
     */
    private void createAdvice(HandRecord handRecord){
        if (handRecord.getHandAdviceList()!=null){
            handAdviceService.deletehandRecordId(handRecord.getId());
            List<HandAdvice> adviceList=new ArrayList<>();
            handRecord.getHandAdviceList().stream().forEach(x->{
                HandAdvice advice=new HandAdvice();
                advice.setId(IdUtils.simpleUUID());
                advice.setHandRecordId(handRecord.getId());
                advice.setNo(x.getNo());
                advice.setPaymentUserId(x.getPaymentUserId());
                advice.setPaymentUserName(x.getPaymentUserName());
                advice.setPaymentTime(x.getPaymentTime());
                advice.setPaymentMoney(x.getPaymentMoney());
                advice.setRoomName(x.getRoomName());
                advice.setRoomId(x.getRoomId());
                advice.setCommunityName(x.getCommunityName());
                advice.setCustomIds(x.getCustomIds());
                advice.setStatus(x.getStatus());
                adviceList.add(advice);
            });
            handAdviceService.doSaveList(adviceList);
        }
    }
}
